package com.yituo.mjworld.mvc.action.user;

import com.yituo.mjworld.core.annotation.Auth;
import com.yituo.mjworld.core.annotation.Check;
import com.yituo.mjworld.core.annotation.NoToken;
import com.yituo.mjworld.core.exception.MyException;
import com.yituo.mjworld.core.web.core.MyHttpRequest;
import com.yituo.mjworld.core.web.core.MyHttpResponse;
import com.yituo.mjworld.mvc.action.IBaseAction;
import com.yituo.mjworld.mvc.entity.web.Product;
import com.yituo.mjworld.mvc.form.DeleteForm;
import com.yituo.mjworld.mvc.form.QueryForm;
import com.yituo.mjworld.mvc.form.QueryGoods;
import com.yituo.mjworld.mvc.form.SearchForm;
import com.yituo.mjworld.mvc.service.impl.ProductServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

/**
 * 产品模块
 */
@Component
public class ProductAction implements IBaseAction {

  @Autowired
  ProductServiceImpl productService;

  /**
   * 获取清单.
   *
   * @param req
   * @param res
   * @throws IOException
   */
  @Override
  @NoToken
  @Check(form = QueryGoods.class)
  public void getList(MyHttpRequest req, MyHttpResponse res) throws IOException {
    QueryForm form = (QueryForm) req.getForm();
    res.success(productService.getList(form));
  }

  /**
   * 删除商品.
   * <p>
   * 继承父类,仅超级管理员可访问
   *
   * @param req
   * @param res
   * @throws IOException
   */
  @Override
  @Auth(admin_user = true)
  @Check(form = DeleteForm.class)
  public void delete(MyHttpRequest req, MyHttpResponse res) throws IOException {
    DeleteForm form = (DeleteForm) req.getForm();
    res.setResult(productService.delete(form));
  }

  /**
   * 添加或修改商品.
   * <p>
   * 继承父类,仅超级管理员可访问
   *
   * @param req
   * @param res
   * @throws IOException
   */
  @Override
  @Check(form = Product.class)
  public void saveOrUpdate(MyHttpRequest req, MyHttpResponse res) throws IOException, InvocationTargetException, IllegalAccessException {
    Product product = (Product) req.getForm();
    res.setResult(productService.saveOrUpdate(product));
  }

  /**
   * 搜索功能
   *
   * @param req
   * @param res
   * @throws IOException
   */
  @NoToken
  @Check(form = SearchForm.class)
  public void search(MyHttpRequest req, MyHttpResponse res) throws IOException {
    res.success(productService.search((SearchForm) req.getForm()));
  }


  /**
   * 更新图片的URL
   * <p>
   * 参数-图片的url为非必填项,为空且原图片不为空时会删除七牛的API
   *
   * @param req
   * @param res
   * @throws IOException
   */
  public void updateUrl(MyHttpRequest req, MyHttpResponse res) throws IOException {
    int id = 0;
    String imgUrl = null;
    try {
      id = Integer.parseInt(req.getParameter("id"));
      imgUrl = req.getParameter("imgUrl");
      if (id < 0) {
        throw new MyException("商品ID不存在!");
      }
      if (StringUtils.isEmpty(imgUrl)) {
        imgUrl = "";
      }
    } catch (Exception e) {
      res.failedFor302("参数错误!");
    }
    res.setResult(productService.updateUrl(id, imgUrl));
  }
}
